﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using KPIS.Utils;
using System.Data;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_BudgetManagementTestDtl : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        int totalNumberOfTextBox = 0;
        int  mng_dtl_seq = 0;
        int mng_seq = 0;
        int pos_type_seq = 0;
        IBudgetManagementTestDtlBLL BudgetManagementTestDtl = new BudgetManagementTestDtlBLL();

     
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            mng_seq = int.Parse(Session["mng_test_seq"].ToString());
            pos_type_seq =int.Parse(Session["pos_type_seq"].ToString());

            DataSet ds = BudgetManagementTestDtl.ListSubScoreDtl(mng_seq, pos_type_seq.ToString()
                , Int32.Parse(Session["PrmSeq"].ToString()), Int32.Parse(Session["JobSiteSeq"].ToString()));

            totalNumberOfTextBox = ds.Tables[0].Rows.Count;

            if (totalNumberOfTextBox > 0)
            {
                lblPosType.Text = "ตำแหน่งประเภท : " + ds.Tables[0].Rows[0].ItemArray[3].ToString();
                mng_dtl_seq = int.Parse(ds.Tables[0].Rows[0].ItemArray[1].ToString());
            }

            if (mng_dtl_seq == 0)
            {
                this.btnUpdate.Visible = false;
                this.btnAddnew.Visible = true;
            }
            else
            {
                this.btnAddnew.Visible = false;
                this.btnUpdate.Visible = true;
            }

            Table t = new Table();
            TableRow tr1 = new TableRow();
            for (int i = 1; i <= totalNumberOfTextBox; i++)
            {
                TableCell tc = new TableCell();
                Label lb = new Label();
                lb.Text = ds.Tables[0].Rows[i-1].ItemArray[5].ToString() + " : ";

                tc.Controls.Add(lb);
                tr1.Cells.Add(tc);
                //Panel1.Controls.Add(lb);
                //Panel1.Controls.Add(tb);
            }

            t.Rows.Add(tr1);
            TableRow tr2 = new TableRow();
            for (int i = 1; i <= totalNumberOfTextBox; i++)
            {
                TableCell tc = new TableCell();
                TextBox tb = new TextBox();
                TextBox tv = new TextBox();
                TextBox ts = new TextBox();
                tb.ID = "MyText" + i.ToString();
                tv.ID = "TextID" + i.ToString();
                ts.ID = "TextSeq" + i.ToString();
                    tb.Width = 100;
               
                tb.Text = ds.Tables[0].Rows[i - 1].ItemArray[6].ToString();
                tv.Text = ds.Tables[0].Rows[i - 1].ItemArray[4].ToString();
                ts.Text = ds.Tables[0].Rows[i - 1].ItemArray[1].ToString();
                
                tv.Visible = false;
                ts.Visible = false;
                tc.Controls.Add(tb);
                tc.Controls.Add(tv);
                tc.Controls.Add(ts);
                tr2.Cells.Add(tc);
                //Panel1.Controls.Add(lb);
                //Panel1.Controls.Add(tb);
            }

            t.Rows.Add(tr2);
            Panel1.Controls.Add(t);
            for (int i = 1; i <= totalNumberOfTextBox; i++)
            {
                TextBox tb = (TextBox)FindControl("MyText" + i.ToString());
                tb.Attributes.Add("onkeypress", "return isNumberKey(event," + tb.ClientID + ")");        // ตัวเลขอย่างเดียว
            }
            
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= totalNumberOfTextBox; i++)
            {
                TextBox text1 = (TextBox)FindControl("MyText" + i.ToString());
                TextBox text2 = (TextBox)FindControl("TextID" + i.ToString());
                BudgetManagementTestDtl.Insert((int)mng_seq, (int)pos_type_seq, int.Parse(text2.Text), double.Parse(text1.Text), 11);

                ScriptManager.RegisterStartupScript(this, this.GetType(), "CloseWindow", "window.frameElement.radWindow.close(true);", true);
            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "CloseWindow", "window.frameElement.radWindow.close(true);", true);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= totalNumberOfTextBox; i++)
            {
                TextBox text1 = (TextBox)FindControl("MyText" + i.ToString());
                TextBox text2 = (TextBox)FindControl("TextID" + i.ToString());
                TextBox text3 = (TextBox)FindControl("TextSeq" + i.ToString());
                BudgetManagementTestDtl.Update(int.Parse(text3.Text) , int.Parse(mng_seq.ToString()), int.Parse(pos_type_seq.ToString()), int.Parse(text2.Text), double.Parse(text1.Text), 11);

                //ScriptManager.RegisterStartupScript(this, this.GetType(), "Refresh", "window.frameElement.radWindow.opener.pagerefresh();", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "CloseWindow", "window.frameElement.radWindow.close(true);", true);
                //Response.Redirect("PR_BudgetManagementTestDtl.aspx");
            }
        }

       
    }
}